/**
 * web-upload 工具类
 *
 * 约定：
 * 上传按钮的id = 图片隐藏域id + 'BtnId'
 * 图片预览框的id = 图片隐藏域id + 'PreId'
 *
 * @author fengshuonan
 */
(function() {

    var $WebFileUpload = function(fileId) {
        this.fileId = fileId;
        this.uploadBtnId = fileId + "BtnId";
        this.uploadPreId = fileId + "PreId";
        this.uploadUrl = Feng.ctxPath + '/mgr/upload';
        this.fileSizeLimit = 100 * 1024 * 1024;
        this.uploadBarId = null;
        this.MD5Id = null;
    };

    $WebFileUpload.prototype = {
        /**
         * 初始化WebFileUploader
         */
        init : function() {
            var uploader = this.create();
            this.bindEvent(uploader);
            return uploader;
        },
        /**
         * 创建WebFileuploader对象
         */
        create : function() {
            var WebFileUploader = WebUploader.create({
                id: this.fileId,
                name : this.fileId,
                auto : true,
                pick : {
                    id : '#' + this.uploadBtnId,
                    multiple : false,// 只上传一个
                },
                accept : {
                    title : this.title,
                    extensions : this.extensions,
                    mimeTypes : this.mimeTypes
                },
                swf : Feng.ctxPath
                + '/static/js/plugins/webuploader/Uploader.swf',
                disableGlobalDnd : true,
                duplicate : true,
                server : this.uploadUrl,
                fileSingleSizeLimit : this.fileSizeLimit
            });

            return WebFileUploader;
        },

        /**
         * 绑定事件
         */
        bindEvent : function(bindedObj) {
            var me =  this;
            bindedObj.on('fileQueued', function(file) {
                bindedObj.md5File(file).then(function (val) {
                    $("#" + me.MD5Id).val(val);
                })
            });

            // 文件上传过程中创建进度条实时显示。
            bindedObj.on('uploadProgress', function(file, percentage) {
                $("#"+me.uploadBarId).css("width",percentage * 100 + "%");
            });

            // 文件上传成功，给item添加成功class, 用样式标记上传成功。
            bindedObj.on('uploadSuccess', function(file,response) {
                Feng.success("上传成功");
                $("#" + me.fileId).val(response);
            });

            // 文件上传失败，显示上传出错。
            bindedObj.on('uploadError', function(file) {
                Feng.error("上传失败");
            });

            // 其他错误
            bindedObj.on('error', function(type) {
                if ("Q_EXCEED_SIZE_LIMIT" == type) {
                    Feng.error("文件大小超出了限制");
                } else if ("Q_TYPE_DENIED" == type) {
                    Feng.error("文件类型不满足");
                } else if ("Q_EXCEED_NUM_LIMIT" == type) {
                    Feng.error("上传数量超过限制");
                } else if ("F_DUPLICATE" == type) {
                    Feng.error("图片选择重复");
                } else {
                    Feng.error("上传过程中出错");
                }
            });

            // 完成上传完了，成功或者失败
            bindedObj.on('uploadComplete', function(file) {
            });
        },

        /**
         * 设置图片上传的进度条的id
         */
        setUploadBarId: function (id) {
            this.uploadBarId = id;
        },
        /**
         * 设置上传完成后md5
         * @param md5Id
         */
        setMD5Id: function(MD5Id) {
            this.MD5Id = MD5Id;
        },
        /**
         * 设置上传文件大小
         * @param fileSizeLimit
         */
        setFileSizeLimit: function (fileSizeLimit) {
            this.fileSizeLimit = fileSizeLimit;
        },
        /**
         * 设置上传文件路径
         * @param uploadUrl
         */
        setUploadUrl: function (uploadUrl) {
            this.uploadUrl = uploadUrl;
        },
        /**
         * 设置上传标题
         * @param title
         */
        setTitle: function (title) {
            this.title = title;
        },
        /**
         * 设置上传文件后缀
         * @param extensions 格式为: 'gif,jpg,jpeg,bmp,png'
         */
        setExtensions: function (extensions) {
            this.extensions = extensions;
        },
        /**
         * 设置上传文件mime types
         * @param mimeTypes 格式为:'image/gif,image/jpg,image/jpeg,image/bmp,image/png'
         */
        setMimeTypes: function (mimeTypes) {
            this.mimeTypes = mimeTypes;
        }
    };

    window.$WebFileUpload = $WebFileUpload;

}());